#import "CTContactTableFooter.h"
#import "CTPrivacyVC.h"
@interface CTContactTableFooter () <M80AttributedLabelDelegate>
@property (nonatomic ,strong) M80AttributedLabel *protocolLabel;
@end
@implementation CTContactTableFooter
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}
- (void)setUI {
    [self addSubview:self.protocolButton];
    [self addSubview:self.protocolLabel];
    [self.protocolButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(Ratio(16));
        make.left.mas_equalTo(Ratio(24));
        make.width.height.mas_equalTo(Ratio(20));
    }];
    [self.protocolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.protocolButton.mas_top).offset(Ratio(2));
        make.left.mas_equalTo(self.protocolButton.mas_right).offset(Ratio(6));
        make.right.mas_lessThanOrEqualTo(self).offset(Ratio(-26));
        make.height.mas_equalTo(Ratio(62));
    }];
}
- (UIButton *)protocolButton
{
    if (_protocolButton == nil) {
        _protocolButton = [[UIButton alloc] init];
        [_protocolButton setImage:[UIImage imageNamed:@"login_unsel"] forState:UIControlStateNormal];
        [_protocolButton setImage:[UIImage imageNamed:@"login_sel"] forState:UIControlStateSelected];
        @weakify(self);
        [[_protocolButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            self.protocolButton.selected = !self.protocolButton.selected;
        }];
    }
    return _protocolButton;
}
- (M80AttributedLabel *)protocolLabel
{
    if (_protocolLabel == nil) {
        NSString *privacyString = @"I have read and agree to the relevant agreement of \"Privacy Policy\" and \"Contact Information Statement\".";
        _protocolLabel = [[M80AttributedLabel alloc]initWithFrame:CGRectZero];
        _protocolLabel.textColor = [UIColor blackColor];
        _protocolLabel.linkColor = [UIColor colorFF6C2E];
        _protocolLabel.backgroundColor = [UIColor clearColor];
        _protocolLabel.highlightColor = [UIColor clearColor];
        _protocolLabel.font = [UIFont systemFontOfSize:13];
        _protocolLabel.underLineForLink = NO;
        _protocolLabel.contentMode = UIViewContentModeLeft;
        _protocolLabel.textAlignment = kCTTextAlignmentLeft;
        _protocolLabel.numberOfLines = 0;
        _protocolLabel.text = privacyString;
        NSRange range1  = [privacyString rangeOfString:@"Privacy Policy"];
        [_protocolLabel addCustomLink:[NSValue valueWithRange:range1] forRange:range1];
        NSRange range2  = [privacyString rangeOfString:@"Contact Information Statement"];
        [_protocolLabel addCustomLink:[NSValue valueWithRange:range2] forRange:range2];
        _protocolLabel.delegate = self;
    }
    return _protocolLabel;
}
#pragma mark - M80label delegate
- (void)m80AttributedLabel:(M80AttributedLabel *)label clickedOnLink:(id)linkData
{
    NSDictionary *data = @{
        @"navigationRoute" : NSStringFromClass(self.class),
        @"pageIdentity" : kct_btn_agreement_detail,
        @"elementUniqueIdentifier"  : kct_btn_agreement_detail,
    };
    [CTUploadManager ct_uploadType:CTUploadTypeBTN data:data];
    NSString *rangeStr =[NSString stringWithFormat:@"%@",linkData];
    NSString *url = @"";
    NSString *title = @"";
    if ([rangeStr containsString:@"14"]){
        NSString *url = [NSURL fullUrlStringForWeb:[CTEnvironmentManager shareManager].privacyPolicyUrl];
        CTPrivacyVC *webVc = [[CTPrivacyVC alloc] initWithTitle:title gotoUrl:url];
        [webVc showLeftSingleBack];
        [[UIViewController currentViewController].navigationController pushViewController:webVc animated:YES];
    }else if ([rangeStr containsString:@"29"]){
        [self showAlert];
    }
}
- (void)showAlert {
    __weak typeof(self) weakself = self;
    TJAlertView *aAlertView = [TJAlertView alertWithTitle:@"Emergency contact Information Statement" subTitle:@"If you need to get in touch with you, our company will give priority to contact you personally. When we cannot contact you, we will only contact the emergency contact you fill in in an emergency situation, and do not use threats or intimidation prohibited by law, and strictly follow the relevant laws and regulations to carry out work. Therefore, when you fill in the emergency contact, you should disclose this information, clearly inform the emergency contact, and obtain their consent. We will not sell, trade or rent your contact information to any third party." clickTitle:@"" clickSecond:@"" click:^(NSString * _Nonnull title) {
    }];
    TJAlertViewAction *aSureAction = [TJAlertViewAction actionWithTitle:@"For more information, please refer to the \"Privacy Policy\"" titleColor:[UIColor whiteColor] backGroudColor:[UIColor colorFF6C2E] handler:^(TJAlertViewAction *action) {
    }];
    [aAlertView doAddAction:aSureAction];
    [aAlertView doShowAlertView];
    return;
}
@end
